<template lang="pug">
data-page(
  v-bind="pageParams"
)
  mt-table(
    v-bind="tableProps"
  )

  edit-form(
    v-bind="editFormProps"
  )
</template>

<script setup lang="ts">
import {
  type FormItemProps,
  type IAction,
  type DataPageProps,
  type Action,
  type EditFormProps,
  type TableProps,
  useNotify,
} from "@scmt/common-components";
import { useRequest } from "@scmt/common-hooks";
import { type QueryPagination, useApi } from "@scmt/core-api";
import type { ProjectEntity, ProjectQueryParams } from "@scmt/api-pacs-project";

const $api = useApi();
const $notify = useNotify();
const queryPramas = reactive<ProjectQueryParams>({
  name: "",
  pageNum: 1,
  pageSize: 20,
});
const editingObj = ref<null | ProjectEntity>(null);

const { request, data, loadingMap } = useRequest({
  tableData: (q: any) => {
    const params = Object.assign({}, queryPramas, q);
    return $api.pacs.project.query(params);
  },
  save: (model: ProjectEntity) => {
    return $api.pacs.project.save(model).then(() => {
      $notify.success("保存成功");
      editingObj.value = null;

      request("tableData", {
        pageSize: 20,
      });
    });
  },
  destroy: (ids: string) => {
    return $api.pacs.project.destroy(ids).then(() => {
      $notify.success("删除成功");

      request("tableData", {
        pageSize: 20,
      });
    });
  },
});

onMounted(() => {
  request("tableData");
});

const pageParams = computed<DataPageProps>(() => {
  const queryFields: FormItemProps[] = [
    {
      prop: "projectName",
      label: "名称",
    },
    {
      prop: "projectCode",
      label: "项目编码",
    },
    {
      prop: "projectType",
      label: "项目类型",
      as: "select",
      options: {
        loadMethod: $api.pacs.inspectionType.listAsOptions,
      },
    },
    {
      prop: "officeId",
      label: "所属科室",
      as: "select",
      options: {
        loadMethod: $api.upms.org.listAllOptions,
        render: "office-table",
      },
    },
  ];

  const headerActions: Action[] = [];

  const dataPageProps: DataPageProps = {
    loading: loadingMap.tableData,
    queryModel: queryPramas,
    queryFields,
    actions: headerActions,
    onQuery: () => request("tableData"),
    onAct: onPageActions,
  };

  return dataPageProps;
});

const tableProps = computed(() => {
  const { tableData } = toRefs(data);

  const tableProps: TableProps = {
    loading: loadingMap.tableData,
    data: tableData.value?.items,
    pagination: tableData.value?.pagination,
    columns: [
      {
        type: "index",
        label: "序号",
        width: "60px",
      },

      {
        prop: "projectName",
        label: "名称",
        tooltip: true,
        minWidth: "120px",
      },
      {
        prop: "projectCode",
        label: "项目编码",
        minWidth: "120px",
      },
      {
        prop: "entryTypeName",
        label: "项目类型",
        minWidth: "120px",
      },
      {
        prop: "basicPrice",
        label: "基础价格(元)",
        minWidth: "120px",
      },
      {
        prop: "officeName",
        label: "所属科室",
        minWidth: "120px",
      },

      {
        prop: "createTime",
        label: "创建时间",
        tooltip: true,
        minWidth: "120px",
        format: "datetime",
      },
      {
        label: "操作",
        fixed: "right",
        width: 80,
        actions: ["edit"],
      },
    ],
    onAct: onPageActions,
    onPaginate: (pagination: QueryPagination) =>
      request("tableData", pagination),
  };
  return tableProps;
});

const editFormProps = computed<EditFormProps>(() => {
  const formItems: FormItemProps[] = [
    {
      prop: "projectName",
      label: "项目名称",
      required: true,
      disabled: true,
    },
    {
      prop: "projectCode",
      label: "项目编码",
      required: true,
      disabled: true,
    },
    {
      prop: "projectType",
      label: "项目类型",
      required: true,
      as: "select",
      options: {
        loadMethod: $api.pacs.inspectionType.listAsOptions,
      },
    },
    {
      prop: "basicPrice",
      label: "基础价格",
      required: true,
      disabled: true,
      options: {
        appendText: "元",
      },
    },
    {
      prop: "officeId",
      label: "所属科室",
      required: true,
      labelKey: "officeName",
      as: "select",
      options: {
        loadMethod: $api.upms.org.listAllOptions,
        render: "office-table",
      },
    },
  ];

  const editProps: EditFormProps = {
    model: editingObj.value,
    items: formItems,
    columns: 1,
    size: "mini",
    loading: loadingMap.save,
    onClose: () => {
      editingObj.value = null;
    },
    onCancel: () => {
      editingObj.value = null;
    },
    onValidate: (model: any) => {
      request("save", model);
    },
  };

  return editProps;
});

const onPageActions = (action: IAction, model?: any) => {
  switch (action.event) {
    case "add":
    case "edit":
      editingObj.value = $api.pacs.project.$$createEntity(model);
      break;
    case "destroy":
      request("destroy", model ? model.id : "");
      break;
  }
};
</script>

<style lang="less" scoped></style>
